#!/usr/bin/env python
# coding: utf-8

# In[62]:


import pandas as pd


# In[63]:


pd.__version__


# ## 上周小结：
# 
# > * 1、[初始高德API（web服务）](https://lbs.amap.com/api/webservice/summary/)
# 
# ## 本节知识点：
# 
# > * 1、[地理编码/逆地理编码](https://lbs.amap.com/api/webservice/guide/api/georegeo)
# > * 2、[路及规划](https://lbs.amap.com/api/webservice/guide/api/direction)
# > * 3、[行政区域查询](https://lbs.amap.com/api/webservice/guide/api/district)
# > * 4、[搜索POI](https://lbs.amap.com/api/webservice/guide/api/search)
# > * 5、[IP定位](https://lbs.amap.com/api/webservice/guide/api/ipconfig)
# > * 6、[静态地图](https://lbs.amap.com/api/webservice/guide/api/staticmaps)
# > * 7、[坐标转换](https://lbs.amap.com/api/webservice/guide/api/convert)
# > * 8、[天气查询](https://lbs.amap.com/api/webservice/guide/api/weatherinfo)
# > * 9、[输入提示](https://lbs.amap.com/api/webservice/guide/api/inputtips)
# > * 10、[交通态势](https://lbs.amap.com/api/webservice/guide/api/trafficstatus)
# 
# 
# 

# ### 准备工作：

# In[64]:


import requests
import json
import pandas as pd
key = "ddcdad5238b943e23a92493fc7541ea8"


# ### 地理编码：
# 
# * API文档：https://lbs.amap.com/api/webservice/guide/api/georegeo
# * API服务地址：https://restapi.amap.com/v3/geocode/geo?parameters
# 
# A.地理编码/逆地理编码 API 是通过 HTTP/HTTPS 协议访问远程服务的接口，提供结构化地址与经纬度之间的相互转化的能力。
# 
# 结构化地址的定义： 首先，地址肯定是一串字符，内含国家、省份、城市、区县、城镇、乡村、街道、门牌号码、屋邨、大厦等建筑物名称。按照由大区域名称到小区域名称组合在一起的字符。一个有效的地址应该是独一无二的。注意：针对大陆、港、澳地区的地理编码转换时可以将国家信息选择性的忽略，但省、市、城镇等级别的地址构成是不能忽略的。暂时不支持返回台湾省的详细地址信息。
# 
# B.必填：key （如：北京市朝阳区阜通东大街6号。如果需要解析多个地址的话，请用"|"进行间隔，并且将 batch 参数设置为 true，最多支持 10 个地址进进行"|"分割形式的请求。）
# 
# C.必填：address （用户在高德地图官网申请Web服务API类型Key）
# 
# D.requests.get()
# 
# E.可选：city（指定查询的城市）、batch（批量查询控制）、sig（数字签名）、output（返回数据格式类型）、callback（回调函数）

# In[286]:


import requests
import json

url = "https://restapi.amap.com/v3/geocode/geo?parameters" 
key = "ddcdad5238b943e23a92493fc7541ea8"
address = "广东省广州市中山大学南方学院"
params = {
    "key":key,
    "address":address,
    "output":"json"
}
r = requests.get(url,params=params)


# In[287]:


r


# In[288]:


r.json()


# In[289]:


中大南方_LOCATION = r.json()['geocodes'][0]['location']
中大南方_LOCATION


# ### 逆地理编码A-2
# 
# * API服务地址：https://restapi.amap.com/v3/geocode/regeo?parameters
# 
# * 关于request参数；
# 
# * 必填；key、location（经纬度坐标）
# 
# * requests.get()

# In[69]:


regeo_rul = "https://restapi.amap.com/v3/geocode/regeo?parameters"
params = {
    "key":key,
    "location":中大南方_LOCATION,
    "extensions":"all",
    "output":"json",
}
r = requests.get(regeo_rul,params=params)


# In[70]:


def regeo(key,location,extensions="base",radius=None,poitype=None):
    url = "https://restapi.amap.com/v3/geocode/regeo?parameters"
    params = {
        "key":key,
        "location":location,
        "poitype":poitype,
        "radius":radius,
        "extensions":extensions,
    }
    r = requests.get(url,params)
    return r.json()


# In[71]:


regeo(key,中大南方_LOCATION,extensions="all",radius=1000)


# #### 编程思维

# In[72]:


list_= []
for i in r.json()['regeocode']['roads']:
    list_.append(i['name'])
list_


# #### 数据思维

# In[73]:


pd.json_normalize(r.json()['regeocode']['pois'])


# ## 路径规划

# * API文档：https://lbs.amap.com/api/webservice/guide/api/weatherinfo
# * 1、步行
# * 2、公交车
# * 3、驾车

# ### A.步行
# * API服务地址：https://restapi.amap.com/v3/direction/walking?parameters
# * requests.get()
# * 参数
# * 必填：key，origin(出发点：经纬度)，destination(目的地：经纬度)

# In[74]:


#函数？！ 地理编码
def geocode(key,address,city=None)->str:
    """获取高德API的地理编码信息
    key是高德web服务中API注册的密钥
    address是结构化地址，参考：https://lbs.amap.com/api/webservice/guide/api/georegeo"""
    url = "https://restapi.amap.com/v3/geocode/geo?parameters" 
    params = {
    "key":key,
    "address":address,
    }
    response = requests.get(url,params=params)
    
    # 返回值
    return response.json()['geocodes'][0]['location']


# In[ ]:





# In[ ]:





# In[75]:


help(geocode)


# In[76]:


address = "广东省广州市中山大学南方学院"
address2 = "广东省广州市从化区龙岗镇"
中大南方_location = geocode(key,address)
中大南方_location


# In[77]:


龙岗_location = geocode(key,address2)
龙岗_location


# In[78]:


help(geocode)


# In[79]:


geocode("ddcdad5238b943e23a92493fc7541ea8","广东省广州市中山大学南方学院")


# In[ ]:





# In[80]:


#获取地理编码（经纬度）

key = "ddcdad5238b943e23a92493fc7541ea8"
address = "广东省广州市中山大学南方学院"
address2 = "广东省广州市从化区龙岗镇"
params = {
    "key":key,
    "address":address,
    "output":"json"
}
r = requests.get(url,params=params)


# In[81]:


url = "https://restapi.amap.com/v3/geocode/geo?parameters" 
params = {
    "key":key,
    "address":address2,
}
r = requests.get(url,params=params)


# In[82]:


url_walking = "https://restapi.amap.com/v3/direction/walking?parameters"
params_walking = {
    "key":key,
    "origin":中大南方_location,
    "destination":龙岗_location,
}
r = requests.get(url_walking,params_walking)
r


# In[83]:


r.json()


# In[84]:


pd.json_normalize(r.json()['route']['paths'][0]['steps'])


# ### B.bus路径
# * API服务地址：https://restapi.amap.com/v3/direction/transit/integrated?parameters

# In[306]:


import pandas as pd
import requests,json
key = "ddcdad5238b943e23a92493fc7541ea8"


# In[307]:


#函数？！ 地理编码
def bus(key,origin,destination,city):
    url_bus = "https://restapi.amap.com/v3/direction/transit/integrated?parameters" 
    params = {
        "key":key,
        "origin":origin,
        "destination":destination,
        "city":city
    }
    r = requests.get(url=url_bus,params=params)
    result = r.json()
    # 返回值
    return result


# In[308]:


location_origin = "113.679287,23.632575"
location_destination = "113.336290,23.115520"
city = "广州" 
bus(key,location_origin,location_destination,city)


# ### C.驾车路径

# * API服务地址：https://restapi.amap.com/v3/direction/driving?parameters

# In[3]:


#函数？！ 地理编码
def car(key,origin,destination,city):
    url_car = "https://restapi.amap.com/v3/direction/driving?parameters" 
    params = {
        "key":key,
        "origin":origin,
        "destination":destination,
        "city":city
    }
    r = requests.get(url=url_car,params=params)
    result = r.json()
    # 返回值
    return result


# In[322]:


location_origin = "113.679287,23.632575"
location_destination = "113.336290,23.115520"
city = "广州" 
car(key,location_origin,location_destination,city)


# ## 行政区域查询

# * API文档：https://lbs.amap.com/api/webservice/guide/api/district
# * API服务地址：https://restapi.amap.com/v3/config/district?parameters
# * 必填：key
# * 可选：keywords、subdistrict、page、offset、extensions、filter、callback、output

# In[97]:


广东省_location = "广东省"
def xzqycx(key,keywords,subdistrict):
    url ='https://restapi.amap.com/v3/config/district?parameters'
    params = {
        "key":key,
        "keywords":keywords,
        "subdistrict":subdistrict
    }
    response = requests.get(url=url,params=params)
    return response.json()


# In[98]:


xzqycx(key,广东省_location,2)


# ## 搜索POI

# * API文档：https://lbs.amap.com/api/webservice/guide/api/search
# * API服务地址：https://restapi.amap.com/v3/place/text?parameters
# * 搜索服务API是一类简单的HTTP接口，提供多种查询POI信息的能力，其中包括关键字搜索、周边搜索、多边形搜索、ID查询四种筛选机制。
# * 必填：key、keywords、types
# * 可选：city……

# In[118]:


中大_location="中山大学"
中大_city="广州"
中大_types="高等院校"


# In[119]:


def poi(key,keywords,types,city):
    url ='https://restapi.amap.com/v3/place/text?parameters'
    params = {
        "key":key,
        "keywords":keywords,
        "types":types,
        "city":city
    }
    response = requests.get(url=url,params=params)
    return response.json()


# In[120]:


poi(key,中大_location,中大_city,中大_types)


# ## IP定位

# * API文档：https://lbs.amap.com/api/webservice/guide/api/ipconfig
# * API服务地址：https://restapi.amap.com/v3/ip?parameters
# * IP定位是一个简单的HTTP接口，根据用户输入的IP地址，能够快速的帮用户定位IP的所在位置。
# * 必填： key
# * 可选： ip

# In[199]:


本机_ip = "61.242.54.114"


# In[200]:


def ip(key,ip):
    url ='https://restapi.amap.com/v3/ip?parameters'
    params = {
        "key":key,
        "ip":ip
    }
    response = requests.get(url=url,params=params)
    return response.json()


# In[201]:


ip(key,本机_ip)


# ## 静态地图
# 

# * API文档：https://lbs.amap.com/api/webservice/guide/api/staticmaps
# * API服务地址：https://restapi.amap.com/v3/staticmap?parameters
# * 静态地图服务通过返回一张地图图片响应HTTP请求，使用户能够将高德地图以图片形式嵌入自己的网页中。用户可以指定请求的地图位置、图片大小、以及在地图上添加覆盖物，如标签、标注、折线、多边形。
# * 必填：key、location（地图中心点（地图中心点））、zoom（地图级别（地图缩放级别:[1,17]））
# 

# In[192]:


from PIL import Image
from io import BytesIO


# In[193]:


map_location="116.48482,39.94858"


# In[194]:


def map(key,location,zoom):
    url = "https://restapi.amap.com/v3/staticmap?parameters"
    params = {
    "key":key,
    "location":location,
    "zoom":zoom
    }
    r = requests.get(url=url,params=params)
    results =Image.open(BytesIO(r.content))
    return results


# In[195]:


map(key,map_location,10)


# ## 坐标转换
# 

# * API文档：https://lbs.amap.com/api/webservice/guide/api/convert
# * API服务地址：https://restapi.amap.com/v3/assistant/coordinate/convert?parameters
# * 坐标转换是一类简单的HTTP接口，能够将用户输入的非高德坐标（GPS坐标、mapbar坐标、baidu坐标）转换成高德坐标。
# * 必填：key、locations（经度和纬度用","分割，经度在前，纬度在后，经纬度小数点后不得超过6位。多个坐标对之间用”|”进行分隔最多支持40对坐标。）

# In[219]:


map_locations="116.48482,39.94858"


# In[220]:


def zbzh(key,locations):
    url = "https://restapi.amap.com/v3/assistant/coordinate/convert?parameters"
    params = {
    "key":key,
    "locations":locations,
    }
    response = requests.get(url=url,params=params)
    return response.json()


# In[221]:


zbzh(key,map_locations)


# ## 天气查询

# * API文档：https://lbs.amap.com/api/webservice/guide/api/weatherinfo
# * API服务地址：https://restapi.amap.com/v3/weather/weatherInfo?parameters
# * 天气查询是一个简单的HTTP接口，根据用户输入的adcode，查询目标区域当前/未来的天气情况。
# * 必填：key，city（城市编码（输入城市的adcode，adcode信息可参考[城市编码表](https://lbs.amap.com/api/webservice/download)））

# In[1]:


wea_city="110101"


# In[2]:


def wea(key,city):
    url = "https://restapi.amap.com/v3/weather/weatherInfo?parameters"
    params = {
    "key":key,
    "city":city,
    }
    response = requests.get(url=url,params=params)
    return response.json()


# In[226]:


wea(key,wea_city)


# ## 输入提示

# * API文档：https://lbs.amap.com/api/webservice/guide/api/inputtips
# * API服务地址：https://restapi.amap.com/v3/assistant/inputtips?parameters
# * 输入提示是一类简单的HTTP接口，提供根据用户输入的关键词查询返回建议列表。
# * 必填：key、keywords（查询关键词）

# In[269]:


sr_keywords="麦当劳"


# In[270]:


def srts(key,keywords):
    url = "https://restapi.amap.com/v3/assistant/inputtips?parameters"
    params = {
        "key":key,
        "keywords":keywords,

    }
    response = requests.get(url=url,params=params)
    return response.json()


# In[271]:


srts(key,sr_keywords)


# In[ ]:




